package cn.fanyamin.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse resp)	
      throws ServletException, IOException {
    doPost(req, resp);
  }
  public void doPost(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException {
    if (isValidUser(req)) {
      String username = req.getParameter("username");
      req.setAttribute("username", username);
      RequestDispatcher rd = req.getRequestDispatcher("/welcome.jsp");
      rd.forward(req, resp);
    }
    else {
        RequestDispatcher rd = req.getRequestDispatcher("/index.jsp");
        rd.forward(req, resp);
    }
  }

  public boolean isValidUser(HttpServletRequest req) {
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    if (username.equals("walter") && password.equals("pass")) {
      return true;
    }
    else {
      return false;
    }
  }
}	
